package com.itbour.web.action.material;

import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.google.gson.Gson;
import com.itbour.com.page.Page;
import com.itbour.common.base.BaseAction;
import com.itbour.common.log.ErrorLogger;
import com.itbour.common.util.OSSUtil;
import com.itbour.common.util.QnFileUtil;
import com.itbour.common.util.StringUtils;
import com.itbour.def.MessageCode;
import com.itbour.def.MessageCodeConstants;
import com.itbour.def.MessageType;
import com.itbour.def.SystemConf;
import com.itbour.dto.com.ImageInfoDto;
import com.itbour.dto.com.OSSTokenInfoDto;
import com.itbour.dto.com.UserInfoDto;
import com.itbour.service.material.ImageService;
import com.opensymphony.xwork2.ActionContext;

/**
 * copyright Itbour team 2015
 * 
 * @author xc
 */

public class GetImageListWebAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	private ImageService imageService;
	private List<ImageInfoDto> imageInfoList;
	private ImageInfoDto imgInfo;
	private String img_name;
	private String url;
	private String delIds;
	private String findUserId;// 查询用
	private String findImgName;// 查询用
	private Map<String, String> uptoken;
	private OSSTokenInfoDto osstoken;
	
	HashMap<String, Object> mapResult = new HashMap<String, Object>(); 

	public String getImageInfo() throws Exception {

		HttpServletRequest request = ServletActionContext.getRequest();
		Map<String, Object> param = new HashMap<String, Object>();
		String imgId = request.getParameter("img_id");
		if (StringUtils.isNotEmpty(imgId)) {
			param.put("img_id", imgId);
		}

		imageInfoList = imageService.selectImageInfo(param);
		return SUCCESS;
	}
	
	public String getImageList() throws Exception {
		// 用户信息
		Map<String, Object> session = ActionContext.getContext().getSession();
    	UserInfoDto user = (UserInfoDto)session.get("user");
    	if(user!=null && user.getUser_id()!=null) {
    		HttpServletRequest request = ServletActionContext.getRequest();
    		String pageNumber = request.getParameter("page");
    		String pageSize = request.getParameter("rows");
    		Map<String, Object> param = new HashMap<String, Object>();
    		if (StringUtils.isNotBlank(findUserId)) {
    			param.put("user_id", findUserId);
    		} else {
    			if(!user.isSuperUser()){
    				param.put("user_id", user.getUser_id());// 默认检索自己上传的图片
    			}
    		}
    		if (StringUtils.isNotEmpty(findImgName)) {
    			param.put("img_name", "%"+findImgName+"%");
    		}
    		Page page = imageService.pageQuery(param, Integer.parseInt(pageNumber),
    				Integer.parseInt(pageSize));
    		HashMap<String, Object> resMap = new HashMap<String, Object>();
    		resMap.put("total", page.getTotalCount());
    		resMap.put("rows", page.getResult());
    		//writeJSON(resMap);
    		mapResult = resMap;
    	} else {
    		mapResult.clear();
    	}
		
		return SUCCESS;
	}
	
	public void deleteImage() throws Exception {
		imageService.deleteImageInfo(delIds);
		this.writeJSON("ok");
	}

	public void addImage() throws Exception {
		if (imgInfo != null) {
			Map<String, Object> session = ActionContext.getContext()
					.getSession();
	    	UserInfoDto user = (UserInfoDto)session.get("user");
	    	if (user != null) {
	    		imgInfo.setUpdate_by(user.getUser_name());
	    		imgInfo.setUser_id(user.getUser_id());
	    	}
	    	imageService.addImage(imgInfo);
		}
		this.writeJSON("ok");
	}

	public String getUpToken() throws Exception {
		Long id = null;
		
		Map<String, Object> session = ActionContext.getContext()
				.getSession();
    	UserInfoDto user = (UserInfoDto)session.get("user");
    	if (user != null) {
    		id = user.getUser_id();
    	}
    	
		String token = QnFileUtil.getToken(QnFileUtil.TYPE_IMAGE, id);
		uptoken = new HashMap<String, String>();
		uptoken.put("uptoken", token);
		
		return SUCCESS;
	}
	
	public String getOSSToken() throws Exception {
		OSSUtil ossUtil = new OSSUtil();
		osstoken = ossUtil.getToken(0);
		
		return SUCCESS;
	}

	/**
	 * 图像保存
	 * 
	 * @return
	 */
	private String saveImageTemp(File image) {
		String imageRootDir = SystemConf.getImageRootDir();
		Date date = new Date();
		DateFormat df1 = new SimpleDateFormat("yyyyMMdd");
		DateFormat df2 = new SimpleDateFormat("yyyyMMddHHmmss");
		String tmpImagePath = "temp/" + df1.format(date) + "/"
				+ df2.format(date) + ".jpg";

		String fileUrl = StringUtils.linkFilePath(imageRootDir, tmpImagePath);
		File saveFile = new File(fileUrl);

		if (image == null) {
			return "";
		}
		try {
			FileUtils.copyFile(image, saveFile);
		} catch (IOException e) {
			ErrorLogger.log(e, new MessageCode(MessageCodeConstants.E_00000001,
					MessageType.ERROR), "RegisterAction", e.getMessage());
			return null;
		} finally {
			image.delete();
		}

		return tmpImagePath;
	}

	public ImageService getImageService() {
		return imageService;
	}

	public void setImageService(ImageService imageService) {
		this.imageService = imageService;
	}

	public List<ImageInfoDto> getImageInfoList() {
		return imageInfoList;
	}

	public void setImageInfoList(List<ImageInfoDto> imageInfoList) {
		this.imageInfoList = imageInfoList;
	}

	public ImageInfoDto getImgInfo() {
		return imgInfo;
	}

	public void setImgInfo(ImageInfoDto imgInfo) {
		this.imgInfo = imgInfo;
	}

	public String getImg_name() {
		return img_name;
	}

	public void setImg_name(String img_name) {
		this.img_name = img_name;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getDelIds() {
		return delIds;
	}

	public void setDelIds(String delIds) {
		this.delIds = delIds;
	}

	public HashMap<String, Object> getMapResult() {
		return mapResult;
	}

	public void setMapResult(HashMap<String, Object> mapResult) {
		this.mapResult = mapResult;
	}

	public Map<String, String> getUptoken() {
		return uptoken;
	}

	public void setUptoken(Map<String, String> uptoken) {
		this.uptoken = uptoken;
	}

	public String getFindUserId() {
		return findUserId;
	}

	public void setFindUserId(String findUserId) {
		this.findUserId = findUserId;
	}

	public String getFindImgName() {
		return findImgName;
	}

	public void setFindImgName(String findImgName) {
		this.findImgName = findImgName;
	}

	public OSSTokenInfoDto getOsstoken() {
		return osstoken;
	}

	public void setOsstoken(OSSTokenInfoDto osstoken) {
		this.osstoken = osstoken;
	}

	
}
